<template>
  <el-row class="login-container" type="flex" justify="center" align="middle">
    <el-col class="login-box" :span="7">
      <p class="title">优点达资讯系统</p>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="login_name">
          <el-input
            v-model="form.login_name"
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            class="login-btn"
            type="primary"
            :loading="loading"
            @click="onSubmit"
          >Login</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script>
export default {
  data() {
    const valiPwd = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('请输入正确的密码'))
      } else {
        callback()
      }
    }
    return {
      // 登录框数据
      form: {
        login_name: 'admin',
        password: '111111'
      },
      loading: false, // 控制登录按钮登录状态
      // 校验规则
      rules: {
        login_name: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: valiPwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 用户登录
    async onSubmit() {
      try {
        await this.$refs.form.validate()
        this.loading = true
        await this.$store.dispatch('user/login', this.form)
        this.$router.push('/')
      } catch (err) {
        console.log(err)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss">
.login-container {
  width: 100%;
  min-height: 100vh;
  background-image: url('~@/assets/loginbg.png');
  background-size: cover;
  .login-box {
    height: 320px;
    background: #fff;
    padding: 0 30px;
    .title {
      line-height: 60px;
      border-bottom: 1px solid #eee;
      color: #ef865b;
      font-weight: normal;
      font-size: 26px;
      text-align: center;
      margin-bottom: 20px;
    }
    .login-btn {
      width: 100%;
    }
    .el-button--primary {
      background-color: coral;
      border-color: coral;
    }
  }
}
</style>
